package com.jeeplus.mob.service;

import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.mob.common.DataConvertHelper;
import com.jeeplus.mob.common.StringHelper;
import com.jeeplus.mob.entity.mobpojo.BuildingInfo;
import com.jeeplus.mob.mapper.BuildingInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Service
public class BuildingService {

    @Autowired
    private BuildingInfoMapper buildingInfoMapper;

    /**
     * 获取所有楼宇名称及楼宇对应的拜访次数
     * @return
     */
    public AjaxJson getAllBuildingNameAndVisit(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> params = DataConvertHelper.getRequestParams(request);
        List<BuildingInfo> buildingInfos = buildingInfoMapper.selectAllBuildingInfo(params);
        if (!StringHelper.IsEmptyOrNull(buildingInfos) && buildingInfos.size() > 0 ) {
            return AjaxJson.success().put("data",buildingInfos);
        }
        return AjaxJson.success("查询出错，请重试！");
    }

    /**
     * 通过楼宇ID获取楼宇信息
     * @param id
     * @return
     */
    public AjaxJson getBuildingNameAndVisitById(String id) {
        BuildingInfo buildingInfo = buildingInfoMapper.selectByPrimaryKey(Integer.parseInt(id));
        if (StringHelper.IsEmptyOrNull(buildingInfo)) {
            return AjaxJson.success("无数据记录，请重试！");
        }
        return AjaxJson.success().put("data",buildingInfo);
    }

    /**
     * 获取并统计首页所有楼宇（合作中、沟通中、禁止进入）拜访次数统计及位置坐标
     * @param request
     * @param response
     * @return
     */
    public AjaxJson getBuildingCountVisitsNumber(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> requestParamsNoToken = DataConvertHelper.getRequestParamsNoToken(request);
        List<BuildingInfo> buildingCountVisitsNumber = buildingInfoMapper.getBuildingCountVisitsNumber(requestParamsNoToken);
        if (buildingCountVisitsNumber.size() > 0 ) {
            return AjaxJson.success().put("data",buildingCountVisitsNumber);
        }
        return AjaxJson.success("无数据结果集，请重试！");
    }

    /**
     * 获取楼宇详情信息
     * @param request
     * @param response
     * @return
     */
    public AjaxJson getBuildingInfomationByLike(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> requestParams = DataConvertHelper.getRequestParamsNoToken(request);
        if (requestParams.containsKey("pageNo") && requestParams.containsKey("pageSize")) {
            Integer pageNo = Integer.parseInt(requestParams.get("pageNo").toString());
            Integer pageSize = Integer.parseInt(requestParams.get("pageSize").toString());
            if ((pageNo == null || pageNo <=0) && (pageSize == null || pageSize <= 0)) {
                pageNo = 1;
                pageNo = 10;
                requestParams.put("pageNo",(pageNo - 1) * pageSize);
                requestParams.put("pageSize",pageSize);
            } else {
                requestParams.put("pageNo",(pageNo - 1) * pageSize);
                requestParams.put("pageSize",pageSize);
            }
        }

        int count = buildingInfoMapper.queryAllBuildingInfoByLikeCount(requestParams);
        List<BuildingInfo> buildingInfos = buildingInfoMapper.queryAllBuildingInfoByLike(requestParams);
        if (StringHelper.IsEmptyOrNull(buildingInfos)) {
            return AjaxJson.success("无数据结果");
        }
        return AjaxJson.success().put("data",buildingInfos).put("count",count);
    }

    /**
     * 获取所有区域信息列表（下拉列表）
     * @return
     */
    public AjaxJson getAllRegionInfomation () {
        String regions = buildingInfoMapper.getAllRegionInfomation();
        if (DataConvertHelper.isNotEmpty(regions)) {
            String[] split = regions.split(",");
            return AjaxJson.success().put("data",split);
        }
        return AjaxJson.success("查询无结果，请重试！");
    }

    /**
     * 获取所有楼宇状态列表（下拉列表）
     * @return
     */
    public AjaxJson getAllBuildingStatus() {
        List<Map<String, Object>> allBuildingStatus = buildingInfoMapper.getAllBuildingStatus();
        if (allBuildingStatus.size() > 0) {
            return AjaxJson.success().put("data",allBuildingStatus);
        }
        return AjaxJson.success("查询无结果，请重试！");
    }
}
